package com.jwthhealth_pub.app.view;

import android.annotation.SuppressLint;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwthhealth_pub.care.CareFragment;
import com.jwthhealth_pub.find.FindFragment;
import com.jwthhealth_pub.health.HealthFragment;
import com.jwthhealth_pub.home.view.HomeFrgment;
import com.jwthhealth_pub.R;
import com.jwthhealth_pub.user.view.CenterFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * main adapter
 * Created by liuzhen on 2017/12/4.
 */
public class MainAdapter extends FragmentPagerAdapter {
    private final MainActivity mActivity;
    private final String[] mTabs;

    private List<ViewHolder> holders;
    private List<Integer> tabIcons, tabSelIcons;

    MainAdapter(FragmentManager fm, MainActivity mainActivity, String[] mainTabs) {
        super(fm);
        this.mActivity = mainActivity;
        this.mTabs = mainTabs;

        tabIcons = new ArrayList<>();
        tabIcons.add(R.mipmap.home_normal);
        tabIcons.add(R.mipmap.health_normal);
        tabIcons.add(R.mipmap.care_normal);
        tabIcons.add(R.mipmap.discover_normal);
        tabIcons.add(R.mipmap.user_normal);

        tabSelIcons = new ArrayList<>();
        tabSelIcons.add(R.mipmap.home_sel);
        tabSelIcons.add(R.mipmap.health_sel);
        tabSelIcons.add(R.mipmap.care_sel);
        tabSelIcons.add(R.mipmap.discover_sel);
        tabSelIcons.add(R.mipmap.user_sel);

        holders = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mTabs.length;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment;

        switch (position) {
            case 0:
                fragment = new HomeFrgment();
                break;
            case 1:
                fragment = new HealthFragment();
                break;
            case 2:
                fragment = new CareFragment();
                break;
            case 3:
                fragment = new FindFragment();
                break;
            case 4:
                fragment = new CenterFragment();
                break;
            default:
                fragment = new NullFragment();
                break;
        }

        return fragment;
    }

    /**
     * 用作自定义选项卡样式
     *
     * @param position pos
     * @return tab view
     */
    View getTabView(int position) {
        @SuppressLint("InflateParams") ViewHolder holder = new ViewHolder(LayoutInflater.from(mActivity).inflate(R.layout.item_home_tab, null));
        holders.add(holder);
        holder.title.setText(mTabs[position]);
        holder.title.setTextColor(mActivity.getResources().getColor(R.color.titleLayoutColor));
        if (position == 0) {
            holder.state(true, position);
        } else {
            holder.state(false, position);
        }

        return holder.view;
    }


    /**
     * 用于切换tab icon的选中状态
     *
     * @param isSelected 是否选中
     * @param tab        tab对象
     */
    void changeState(boolean isSelected, TabLayout.Tab tab) {

        if (tab != null) {
            int pos = tab.getPosition();

            if (isSelected) {
                ViewHolder selHolder = holders.get(pos);
                selHolder.state(true, pos);
                tab.setCustomView(selHolder.getView());
            } else {
                ViewHolder unSelHolder = holders.get(pos);
                unSelHolder.state(false, pos);
                tab.setCustomView(unSelHolder.getView());
            }
        }

    }

    private class ViewHolder {

        private View view;

        private TextView title;
        private ImageView icon;

        ViewHolder(View view) {
            this.view = view;

            icon = view.findViewById(R.id.iv_item_home_tab);
            title = view.findViewById(R.id.tv_item_home_tab);
        }

        public View getView() {
            return view;
        }

        void state(boolean isSelected, int position) {
            if (isSelected) {
                title.setTextColor(mActivity.getResources().getColor(R.color.titleLayoutColor));
//                title.setTextColor(Color.WHITE);
                icon.setBackgroundResource(tabSelIcons.get(position));
            } else {
                title.setTextColor(mActivity.getResources().getColor(R.color.textColorGray));
                icon.setBackgroundResource(tabIcons.get(position));
            }
        }
    }
}
